package com.wenhuang.chatgptopen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wenhuang.chatgptopen.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/*
 *@Time：2023/5/20
 *@Author：Jelly
 */
public interface UserMapper extends BaseMapper<User> {

    //向数据库添加用户
    @Insert("insert into user(id,email,password,username) values (null,#{email},#{password},#{username})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    Integer addUser(User user);

    //通过邮箱和密码查询用户
    @Select("select * from user where email = #{email} and password = #{password}")
    User getUserByEmailAndPwd(User user);

    //根据邮箱查询用户（检验邮箱是否存在）
    @Select("select email from user where email = #{email}")
    User verifyEmail(User user);

    //查询头像路径
    @Select("select avatar from user where email = #{email}")
    String getAvatar(User user);

    //修改头像路径
    @Update("update user set avatar = #{path} where id = #{id}")
    Integer changeAvatar(String path,Integer id);

    //修改用户名
    @Update("update user set username = #{username} where id = #{id}")
    Integer changeUsername(String username,Integer id);

    //修改密码
    @Update("update user set password = #{password} where id = #{id}")
    Integer changePassword(String password,Integer id);

    //查询用户名
    @Select("select username from user where username = #{username}")
    String selectUsername(String username);

    //根据id查询用户所有信息
    @Select("select * from user where id = #{id}")
    User getUserById(User user);

    //粉丝数+1
    @Update("update user set fans = #{fans} + 1 where id = #{id}")
    Integer changFans(User user);

    //粉丝数-1
    @Update("update user set fans = #{fans} - 1 where id = #{id}")
    Integer deleteFans(User user);

    //根据用户查询用户
//    @Select("select * from user where username = #{username}")
    @Select("select * from user where username like '%' #{username} '%' ")
    List<User> searchUser(User user);
}
